<template>
  <div :class="className" :style="{'height':'100%','width':'100%'}" />
</template>

<script>
  import * as echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme
  import resize from '../../views/dashboard/mixins/resize';
  const animationDuration = 1000

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '500px'
      },
      chartData: {
        type: Object,
        required: true
      }
    },
    data() {
      return {
        chart: null
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el)
        this.setOption(this.chartData)
      },
      setOption({data}={}){
        this.chart.setOption({
          title:{
            text: '',
            textStyle:{
              fontSize: 16,
              fontWeight:400,
              color:'#fff'
            }
          },
          legend: {
            left: 'top',
            padding:[5,0,0,10],
            textStyle:{
              color:"#fff"
            }
          },
          grid: {
            top: '20',
            left: '20',
            right: '30',
            bottom: '3%',
            containLabel: true
          },
          series: [
            {
              type: 'pie',
              radius: [15, 70],
              center: ['50%', '55%'],
              roseType: 'area',
              label:{
                color:'#fff',
                lineHeight:20,
                formatter:'{b} : {c}\n {d}%'
              },
              itemStyle: {
                borderRadius: 0
              },
              data: data
            }
          ]
        })
      }
    },
    watch: {
      chartData: {
        deep: true,
        handler(val) {
          this.setOption(val)
        }
      }
    },
  }
</script>
